package com.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class TextureAtlas {

	Texture texture;
	float textureSize;
	float textureHeinght;
	float textureWidth;

	public TextureAtlas(String texturePath)
	{
		try {
			texture = TextureLoader.getTexture("PNG",
						new FileInputStream(new File(texturePath)));
		} catch (IOException e) {
			e.printStackTrace();
		}
		textureSize = texture.getImageWidth()/texture.getImageHeight();
		textureHeinght = texture.getImageHeight();
		textureWidth = texture.getImageWidth();
	}
	public void bindAtlas()
	{
		texture.bind();
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
	}
	public int getMaxId()
	{
		return (int) ((texture.getHeight()/textureSize)*(texture.getHeight()/textureSize));
	}
	public float[][] getTextCoords(byte textureId)
	{
		return new float[][]{
				{textureId/textureSize,1},
				{(textureId+1)/textureSize,1},
				{(textureId+1)/textureSize,0},
				{textureId/textureSize,0}		
		};
	}
}
